﻿/// <reference path="Entity.js" />

function GetSqliteDataTypeForMSSql(Type) {
    ///<summary>把MSSqlServer的数据类型转化为Sqlite的数据类型</summary>
    ///<param name="Type" type="string">MSSqlServer的数据类型</param>
    ///<returns type="String">Sqlite数据类型</returns>
    var sqliteType = "text";
    switch (Type) {
        case "int": sqliteType = "integer"; break;
        case "tinyint": sqliteType = "integer"; break;
        case "smallint": sqliteType = "integer"; break;
        case "float": sqliteType = "real"; break;
        case "money": sqliteType = "real"; break;
        case "real": sqliteType = "real"; break;
        case "decimal": sqliteType = "real"; break;
        case "numeric": sqliteType = "real"; break;
        case "smallmoney": sqliteType = "real"; break;
        default: sqliteType = "text"; break;
    }
    return sqliteType;
}

function Insert() {
    var sqlStr = "insert into " + this.TableInfo.Name + " (";
    var propertyString = "";
    var valParamertString = "";
    for (var i = 0; i < this.TableInfo.Column.length; i++) {
        var Column = this.TableInfo.Column[i];
        valParamertString += ",?";
        propertyString += "," + Column.Name;
    }
    sqlStr += propertyString.substring(1);
    sqlStr += ") values (" + valParamertString.substring(1) + ");";
    return sqlStr;
}
function CreateTable() {
    var sqlStr = "create table " + this.TableInfo.Name + " (";
    for (var i = 0; i < this.TableInfo.Column.length; i++) {
        var Column = this.TableInfo.Column[i];
        sqlStr += Column.Name + " " + GetSqliteDataTypeForMSSql(Column.Type) + ",";
    }
    sqlStr = sqlStr.substring(0, sqlStr.length - 1) + ");";
    return sqlStr;
}
function Update() {
    var sqlStr = "update " + this.TableInfo.Name + " set ";
    for (var i = 0; i < this.TableInfo.Column.length; i++) {
        var Column = this.TableInfo.Column[i];
        if (Column.Name != this.TableInfo.PrimaryKey) {
            sqlStr += Column.Name + "=?,";
        }
    }
    sqlStr = sqlStr.substring(0, sqlStr.length - 1) + " where " + this.TableInfo.PrimaryKey + "=?;";
    return sqlStr;
}
function Delete() {
    var sqlStr = "delete " + this.TableInfo.Name + " where " + this.TableInfo.PrimaryKey + "=?;";
    return sqlStr;
}
function GetInsertValueArray() {
    var propertyArray = [];
    for (var i = 0; i < this.TableInfo.Column.length; i++) {
        var Column = this.TableInfo.Column[i];
        propertyArray.push(this[Column.Name]);
    }
    return propertyArray;
}
function GetUpdateValueArray() {
    var propertyArray = [];
    for (var i = 0; i < this.TableInfo.Column.length; i++) {
        var Column = this.TableInfo.Column[i];
        if (Column.Name != this.TableInfo.PrimaryKey) {
            propertyArray.push(this[Column.Name]);
        }
    }
    propertyArray.push(this.TableInfo.PrimaryKey);
    return propertyArray;
}
function GetDeleteValueArray() {
    var propertyArray = [];
    propertyArray.push(this.TableInfo.PrimaryKey);
    return propertyArray;
}
function GetPropertyString() {
    var propertyStr = "";
    for (var i = 0; i < this.TableInfo.Column.length; i++) {
        var Column = this.TableInfo.Column[i];
        propertyStr += "," + Column.Name;
    }
    return propertyStr.substring(1);
}
function GetJsonString() {
    var JsonString = "{";
    for (var i = 0; i < this.TableInfo.Column.length; i++) {
        var Column = this.TableInfo.Column[i];
        JsonString += "\"" + Column.Name + "\":\"" + this[Column.Name] + "\",";
    }
    JsonString = JsonString.substring(0, JsonString.length - 1) + "}";
    return JsonString;
}
function SetJsonString(JsonString) {
}